// 构造函数与析构函数
#include <iostream>
#include <string>
using namespace std;
// 创建类时，编译器会创建默认构造函数、拷贝构造函数、析构函数
// 拷贝构造函数默认拷贝所有成员变量
// 只要自己写了任意一种构造函数，编译器则不再提供
// 当其他类作为此类的成员变量时，先构造其他类，再构造此类
class Student {
private:
    string m_name;
    int m_id;
public:
    // 构造函数
    Student () {
        this->m_name = "no_name";
        cout << "无参构造" << endl;
    }
    Student (string name) {
        this->m_name = name;
        cout << "有参构造" << endl;
    }
    // 拷贝构造函数
    Student (const Student &s) {
        this->m_name = s.m_name;
        cout << "拷贝构造" << endl;
    }
    // 析构函数
    ~Student() {
        cout << "调用析构函数" << endl;
    }
    void setName(string name) {
        this->m_name = name;
    }
    void showName() {
        cout << "name: " << m_name << endl;
    }
    void showId() {
        cout << "id: " << m_id << endl;
    }
    
};
void doWork1(Student s) {
    // 以值传递时会调用拷贝构造函数
}
Student doWork2(void) {
    // 返回值时会调用拷贝构造函数(实际并不是这样)
    Student s2;
    cout << (int*)&s2 << endl;
    return s2;
}
void construct() {
    // 括号法构造
    // Student s1;
    // Student s2("lxy");
    // Student s3(s2);
    // 显式法
    // Student s4 = Student("lxy");
    // Student s5 = Student(s2);
    // 匿名对象未被使用会被立刻销毁

    // doWork1(s1);
    Student s3 = doWork2();
    cout << (int*)&s3 << endl;

}
int main(void) {
    construct();

    return 0;
}